<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="server," />





  <link rel="alternate" href="/atom.xml" title="GrantonZhuang's Blog" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="记录服务器管理和安全工作方面上的一些笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="服务器安全笔记">
<meta property="og:url" content="https://grantonzhuang.github.io/2016/10/16/serverSafeNote/index.html">
<meta property="og:site_name" content="GrantonZhuang's Blog">
<meta property="og:description" content="记录服务器管理和安全工作方面上的一些笔记">
<meta property="og:updated_time" content="2016-10-21T12:15:40.501Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="服务器安全笔记">
<meta name="twitter:description" content="记录服务器管理和安全工作方面上的一些笔记">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="https://grantonzhuang.github.io/2016/10/16/serverSafeNote/"/>

  <title> 服务器安全笔记 | GrantonZhuang's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">GrantonZhuang's Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/index.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                服务器安全笔记
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-10-16T15:36:10+08:00" content="2016-10-16">
              2016-10-16
            </time>
          </span>

          

          
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>记录服务器管理和安全工作方面上的一些笔记</p>
<a id="more"></a>
<h1 id="0x03-git-安全管理"><a href="#0x03-git-安全管理" class="headerlink" title="0x03 git 安全管理"></a>0x03 git 安全管理</h1><ul>
<li><p>创建一个 git 用户专门用以 git 使用</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo adduser git</div></pre></td></tr></table></figure>
</li>
<li><p>禁用 git 的 shell 登陆, 上面一行是修改之前, 后面一行是修改之后</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">cat /etc/passwd | grep git</div><div class="line">git:x:1000:1000:,,,:/home/git:/bin/bash</div><div class="line">cat /etc/passwd | grep git</div><div class="line">git:x:1000:1000:,,,:/home/git:/usr/bin/git-shell</div></pre></td></tr></table></figure>
</li>
<li><p>git 用户的 .ssh 文件夹的权限情况 (700)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">/home/git<span class="comment"># ls -al</span></div><div class="line">drwx------ 2 git  git  4096 Jul 19 10:17 .ssh</div></pre></td></tr></table></figure>
</li>
<li><p>authorized_keys 的权限情况 (600)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">/home/git/.ssh<span class="comment"># ls -l</span></div><div class="line">-rw------- 1 git   git   1933 Jul 19 10:15 authorized_keys</div></pre></td></tr></table></figure>
</li>
<li><p>仓库文件的用户和用户组 (git:git)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">srv<span class="comment"># ls -l</span></div><div class="line">drwxr-xr-x 7 git git 4096 Jul  4 17:04 web</div></pre></td></tr></table></figure>
</li>
<li><p>仓库地址的格式</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">git@server:/path/to/file</div></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="0x02-创建新用户-并且使用-public-key-登陆"><a href="#0x02-创建新用户-并且使用-public-key-登陆" class="headerlink" title="0x02 创建新用户, 并且使用 public key 登陆"></a>0x02 创建新用户, 并且使用 public key 登陆</h1><blockquote>
<p>在 0x01 之前, 进行这一步操作</p>
</blockquote>
<ul>
<li><p>添加用户</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo adduser newuser</div></pre></td></tr></table></figure>
</li>
<li><p>在这个用户的主目录下, 将 ssh 的 public key 添加进来</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">tree</div><div class="line">.</div><div class="line">├── authorized_keys2</div><div class="line">├── keyOne.pub</div><div class="line">├── keyTwo.pub</div><div class="line">└── keyThree.pub</div></pre></td></tr></table></figure>
</li>
<li><p>添加一个 key 到 authorized_keys2 的方法是</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">echo</span> key &gt;&gt; authorized_keys2</div></pre></td></tr></table></figure>
</li>
<li><p>从本地机器 copy 文件到远程机器, 注意权限问题</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp /path/to/localfile username@server:/path/to/destination</div></pre></td></tr></table></figure>
</li>
<li><p>从远程机器 copy 文件到本地机器, 注意权限问题</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp username@server:/path/to/file /path/to/<span class="built_in">local</span>Destination</div></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="0x01-禁用-root-登陆"><a href="#0x01-禁用-root-登陆" class="headerlink" title="0x01 禁用 root 登陆"></a>0x01 禁用 root 登陆</h1><blockquote>
<p>修改配置文件, 禁用 root 用户登陆, 同时禁用密码登陆</p>
</blockquote>
<ul>
<li><p>配置文件的路径在这里</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> /etc/ssh/</div></pre></td></tr></table></figure>
</li>
<li><p>备份 config 文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp sshd_config sshd_config.bak</div></pre></td></tr></table></figure>
</li>
<li><p>修改过后的文件</p>
</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">diff sshd_config.bak sshd_config</div><div class="line">85c85</div><div class="line">&lt; PermitRootLogin yes</div><div class="line">---</div><div class="line">&gt; PermitRootLogin no</div><div class="line">87c87</div><div class="line">&lt; PasswordAuthentication yes</div><div class="line">---</div><div class="line">&gt; PasswordAuthentication no</div></pre></td></tr></table></figure>
<h1 id="0x00-MySQL-数据库管理"><a href="#0x00-MySQL-数据库管理" class="headerlink" title="0x00 MySQL 数据库管理"></a>0x00 MySQL 数据库管理</h1><blockquote>
<p>在服务器上创建一个数据库, 以及创建用户和密码, 授予数据库的权限给这个用户</p>
</blockquote>
<ul>
<li><p>user local login</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">CREATE</span> <span class="keyword">DATABASE</span> dbName;</div><div class="line"><span class="keyword">CREATE</span> <span class="keyword">USER</span> <span class="string">'userName'</span>@<span class="string">'localhost'</span> <span class="keyword">IDENTIFIED</span> <span class="keyword">BY</span> <span class="string">'password'</span>;</div><div class="line"><span class="keyword">GRANT</span> ALL <span class="keyword">PRIVILEGES</span> <span class="keyword">ON</span> dbName.* <span class="keyword">TO</span> <span class="string">'userName'</span>@<span class="string">'localhost'</span>;</div></pre></td></tr></table></figure>
</li>
<li><p>user remote login</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">CREATE</span> <span class="keyword">DATABASE</span> dbName;</div><div class="line"><span class="keyword">CREATE</span> <span class="keyword">USER</span> <span class="string">'userName'</span>@<span class="string">'%'</span> <span class="keyword">IDENTIFIED</span> <span class="keyword">BY</span> <span class="string">'password'</span>;</div><div class="line"><span class="keyword">GRANT</span> ALL <span class="keyword">PRIVILEGES</span> <span class="keyword">ON</span> dbName.* <span class="keyword">TO</span> <span class="string">'userName'</span>@<span class="string">'%'</span>;</div></pre></td></tr></table></figure>
</li>
<li><p>remote permissions</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">REVOKE</span> ALL <span class="keyword">PRIVILEGES</span> <span class="keyword">ON</span> dbName.* <span class="keyword">FROM</span> <span class="string">'userName'</span>@<span class="string">'localhost'</span>;</div><div class="line"><span class="keyword">REVOKE</span> ALL <span class="keyword">PRIVILEGES</span> <span class="keyword">ON</span> dbName.* <span class="keyword">FROM</span> <span class="string">'userName'</span>@<span class="string">'%'</span>;</div></pre></td></tr></table></figure></li>
</ul>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/server/" rel="tag">#server</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/10/05/jniNote/" rel="next" title="jni 笔记">
                <i class="fa fa-chevron-left"></i> jni 笔记
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/10/22/ViewSQLite/" rel="prev" title="Android SQLite 工具记录">
                Android SQLite 工具记录 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://ww1.sinaimg.cn/mw690/6e8939a6gw1f3g1u1mc6lj20hs0hsmxf.jpg"
               alt="Granton Zhuang" />
          <p class="site-author-name" itemprop="name">Granton Zhuang</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives/index.html">
              <span class="site-state-item-count">26</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#0x03-git-安全管理"><span class="nav-number">1.</span> <span class="nav-text">0x03 git 安全管理</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#0x02-创建新用户-并且使用-public-key-登陆"><span class="nav-number">2.</span> <span class="nav-text">0x02 创建新用户, 并且使用 public key 登陆</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#0x01-禁用-root-登陆"><span class="nav-number">3.</span> <span class="nav-text">0x01 禁用 root 登陆</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#0x00-MySQL-数据库管理"><span class="nav-number">4.</span> <span class="nav-text">0x00 MySQL 数据库管理</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Granton Zhuang</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  



  




  
  

  

  

  

</body>
</html>
